package mainWindowSupport;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import java.io.Serializable;
import actionHandlers.PropertiesTableListener;
import cuShapes.PQShape;
import cuShapes.ShapeList;
import cuShapes.ShapeListObserver;

public class ShapePropTable implements ShapeListObserver, Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3141157359729455340L;
	JTable shapeTable;
	TableModelListener propModelListener;
	
	public ShapePropTable() {
			
		shapeTable = new JTable();
		List<String> rows = new ArrayList<String>();
		rows.add("Text");
		rows.add("X");
		rows.add("Y");
		rows.add("Width");
		rows.add("Height");
		rows.add("Color");
		propModelListener = new PropertiesTableListener(shapeTable);
		initComponents(shapeTable, rows);
	}
	
	@SuppressWarnings("serial")
	private void initComponents(JTable table, List<String> numRows) {

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [numRows.size()][2],
            new String [] {"General", ""}
        ) {
            Class<?>[] types = new Class<?>[] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class<?> getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        for(int i=0;i<numRows.size();i++)
        	table.getModel().setValueAt(numRows.get(i), i, 0);
        shapeTable.getModel().addTableModelListener(propModelListener);
    }
	
	public void setProperties(PQShape shape) {		
		if (shape!=null) {
			shapeTable.getModel().setValueAt(String.valueOf(shape.getWidth()), 3, 1);
			shapeTable.getModel().setValueAt(String.valueOf(shape.getHeight()), 4, 1);
			shapeTable.getModel().setValueAt(shape.getColor(), 5, 1);		
			shapeTable.getModel().setValueAt(shape.getName(), 0, 1);
			shapeTable.getModel().setValueAt(String.valueOf(shape.getStart().getX()), 1, 1);
			shapeTable.getModel().setValueAt(String.valueOf(shape.getStart().getY()), 2, 1);
		}
	}
	
	public JTable getTable() {
		return shapeTable;
	}
	
	public void clear() {
		for(int i=0; i<shapeTable.getModel().getRowCount();i++)
			shapeTable.getModel().setValueAt("", i, 1);
		
		shapeTable.updateUI();
	}

	public void update(ShapeList s) {
		shapeTable.getModel().removeTableModelListener(propModelListener);
		setProperties(s.getCurrentShape());
		shapeTable.getModel().addTableModelListener(propModelListener);
	}
}
